import data from '@/area.js'
import utils from '@/utils'

const province = data.province
const city = data.city
const district = data.district

const list = [province, city, district]

function getChildren(pCode, child) {
    return child.filter(item => item.parent_code === pCode)
}

export default function (level = 3) {
    if (![1, 2, 3].includes(Number(level))) return []
    // 反转是因为，级别小的必须排前面。。
    const _list = utils.deepClone([...list].splice(0, level).reverse())
    for (let i = 0; i < _list.length - 1; i++) {
        _list[i + 1].forEach(item => {
            item.children = getChildren(item.area_code, _list[i])
        })
    }
    return _list[_list.length - 1]
}
